﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mmoss.orm;
using mmoss.application.classes;
using mmoss.configuration;

namespace mmoss.application
{
    public partial class frmSubMenu : Form
    {
        public CartItem ChosenCartItem
        {
            get
            {
                CartItem l_item = new CartItem();
                l_item.InventoryItemByVendorMMC = ChosenItem;
                l_item.itemPrice = l_item.itemPrice;
                l_item.Quantity = Quant;
                return l_item;
            }
        }
        //quantity unit from type table
        public decimal QuantUnit { get; set; }
        //item which is passed back
        public InventoryItemByVendorMMC ChosenItem { get; set; }
        public decimal Quant { get; set; }
        public InventoryItemType CurrentType { get; set; }
        public frmSubMenu(InventoryItemType l_type)
        {
            CurrentType = l_type;
            InitializeComponent();
            dgSubItems.AutoGenerateColumns = false;
            this.Load += new EventHandler(frmSubMenu_Load);
            loadSubItemGrid();

            cboQuant.DataSource = dbConn.db.InventoryQuantTypes.Where(p=>p.inventoryTypeID==1).ToList();
            cboQuant.ValueMember = "quantTypeID";
            cboQuant.DisplayMember = "descShort";
            cboQuant.SelectedIndexChanged += new EventHandler(cboQuant_SelectedIndexChanged);
            cboQuant.SelectedIndex = 0;
            recalcQuant();
        }
        private void loadSubItemGrid()
        {
            dgSubItems.DataSource = dbConn.db.InventoryItemByVendorMMCS.GetItemsByType(
                Default.CurrentLocation.Id
                , CurrentType
                );
            if (dgSubItems.Rows.Count > 0)
            {
                //unselect all first
                unselectAllGridRows();
                //select first row
                dgSubItems.Rows[0].Selected = true;
                showRowLabel();
            }
        }
        private void unselectAllGridRows()
        {
            foreach (DataGridViewRow row in dgSubItems.Rows)
            {
                row.Selected = false;
            }
        }
        private void recalcQuant()
        {
            txtQuant.Text = ((InventoryQuantType)cboQuant.SelectedItem).Quant.ToString();
        }
        void cboQuant_SelectedIndexChanged(object sender, EventArgs e)
        {
            recalcQuant();
        }
        public frmSubMenu()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmSubMenu_Load);
            dgSubItems.AutoGenerateColumns = false;
        }

        void frmSubMenu_Load(object sender, EventArgs e)
        {
            //load menu for items
            //load 'favorite items' buttons
            loadFavoriteButtons();
            dgSubItems.SelectionChanged += new EventHandler(dgSubItems_SelectionChanged);
            //lblSelectedItemName.Text = "None";
            QuantUnit = decimal.Parse("0.1");
        }
        private void showRowLabel()
        {
            if (dgSubItems.SelectedRows.Count > 0)
            {
                lblSelectedItemName.Text = ((InventoryItemByVendorMMC)dgSubItems.SelectedRows[0].DataBoundItem).ItemName;
            }
            else
            {
                lblSelectedItemName.Text = "Please selected";
            }
        }
        void dgSubItems_SelectionChanged(object sender, EventArgs e)
        {
            //new row selected
            showRowLabel();
        }
        private void loadFavoriteButtons()
        {
            //load the favorite items buttons with top six choices
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            decimal result;
            if (!decimal.TryParse(txtQuant.Text, out result))
            {
                MessageBox.Show("Please enter valid quantity");
                txtQuant.Select();
                return;
            }
            if (dgSubItems.SelectedRows[0] == null)
            {
                MessageBox.Show("Please select an item");
                return;
            }
            ChosenItem = (InventoryItemByVendorMMC) dgSubItems.Rows[dgSubItems.SelectedRows[0].Index].DataBoundItem;
            Quant = result;
            this.Close();
        }


        private void btnIncQuant_Click(object sender, EventArgs e)
        {
            txtQuant.Text = (decimal.Parse(txtQuant.Text) + QuantUnit).ToString();
        }

        private void btnDecQuant_Click(object sender, EventArgs e)
        {
            txtQuant.Text = (decimal.Parse(txtQuant.Text)>0?(decimal.Parse(txtQuant.Text) - QuantUnit).ToString():decimal.Parse("0").ToString());
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            frmAdminInventoryItemVendor l_addItem = new frmAdminInventoryItemVendor();
            l_addItem.ItemType = CurrentType;
            l_addItem.FormClosed += new FormClosedEventHandler(l_addItem_FormClosed);
            l_addItem.ShowDialog();
        }
        private void selectItem(InventoryItemByVendorMMC l_item)
        {

        }
        void l_addItem_FormClosed(object sender, FormClosedEventArgs e)
        {
            //refresh the items available 
            loadSubItemGrid();
        }

        private void btnIncQuantGroup_Click(object sender, EventArgs e)
        {
            cboQuant.SelectedIndex = (cboQuant.SelectedIndex == cboQuant.Items.Count-1 ? 0: cboQuant.SelectedIndex+1);
        }

        private void btnDecQuantGroup_Click(object sender, EventArgs e)
        {
            cboQuant.SelectedIndex = (cboQuant.SelectedIndex == 0 ? cboQuant.Items.Count - 1 : cboQuant.SelectedIndex-1);
        }

        private void btnIncItem_Click(object sender, EventArgs e)
        {
            //increment the item selected in the subitem grid
            if (dgSubItems.SelectedRows[0].Index == (dgSubItems.Rows.Count-1))
            {
                unselectAllGridRows();
                dgSubItems.Rows[0].Selected = true;
                return;
            }
            dgSubItems.Rows[dgSubItems.SelectedRows[0].Index + 1].Selected = true;
        }

        private void btnDecItem_Click(object sender, EventArgs e)
        {
            if (dgSubItems.SelectedRows[0].Index == 0)
            {
                unselectAllGridRows();
                dgSubItems.Rows[dgSubItems.Rows.Count - 1].Selected = true;
                return;
            }
            dgSubItems.Rows[dgSubItems.SelectedRows[0].Index - 1].Selected = true;

        }
    }
}
